package io.renren.common.aspect;

import io.renren.common.utils.DingTalkService;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class ExceptionNotificationAspect {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionNotificationAspect.class);

    @Autowired
    private DingTalkService dingTalkService;

    @AfterThrowing(pointcut = "execution(* io.renren.modules..*(..))", throwing = "ex")
    public void handleException(Exception ex) {
        String errorMessage = "【异常警告】\n" + "错误信息：" + ex.getMessage() + "\n" + "堆栈信息：" + Arrays.toString(ex.getStackTrace());
        logger.error(errorMessage, ex);
        dingTalkService.sendTextMessageWithSignature(errorMessage);
    }
}
